// 缓存操作
const locationF = (type, name, ids, datas) => {
  if (type == "set") {
    let info = localStorage.getItem("tableListLocation")
      ? JSON.parse(localStorage.getItem("tableListLocation"))
      : {};
    info = {
      ...info,
      [ids]: {
        ...info[ids],
        [name]: datas,
      },
    };
    localStorage.setItem("tableListLocation", JSON.stringify(info));
  } else if (type == "get") {
    let info = localStorage.getItem("tableListLocation")
      ? JSON.parse(localStorage.getItem("tableListLocation"))
      : {};
    if (info && info[ids] && info[ids][name]) {
      return info[ids][name];
    } else {
      return null;
    }
  }
};
const routeAuthority = (o, t) => {
  const getMenuActiveIds = (arr) => {
    if (Array.isArray(arr) && arr.length > 0) {
      if (Array.isArray(arr[0].child) && arr[0].child.length > 0) {
        return getMenuActiveIds(arr[0].child);
      } else {
        return arr[0].id;
      }
    }
  };
  let routerObj = {};
  let menuList = localStorage.getItem("menuList")
    ? JSON.parse(localStorage.getItem("menuList"))
    : [];
  let menuArr = localStorage.getItem("menu") ? JSON.parse(localStorage.getItem("menu")) : [];
  if (menuList.length == 0 || menuArr.length == 0) {
    return;
  }
  // 路由变化时执行的操作
  if (o.path == "/tables" && !o.query.id) {
    if (menuList.length > 0) {
      if (menuList[0].name == "首页") {
        routerObj["path"] = "/manage";
      } else {
        routerObj["path"] = "/tables";
        routerObj["query"] = {
          id: getMenuActiveIds(menuList),
        };
      }
    } else {
      routerObj["path"] = "/manage";
    }
  } else {
    if (o.path == "/manage") {
      let findItem = menuArr.find((val) => val.name == "首页");
      if (!findItem) {
        routerObj["path"] = "/tables";
        routerObj["query"] = {
          id: getMenuActiveIds(menuList),
        };
      }
    } else if (o.path == "/tables") {
      let findItem = menuArr.find((val) => val.id == o.query.id);
      // 判断是不是开发环境  -- 不受菜单的限制 location
      let isDevBoolean = location.host.includes("localhost:") || location.host.includes(":8080");
      if (!findItem && !isDevBoolean) {
        if (menuList[0].name == "首页") {
          routerObj["path"] = "/manage";
        } else {
          routerObj["path"] = "/tables";
          routerObj["query"] = {
            id: getMenuActiveIds(menuList),
          };
        }
        t.$message.error({
          content: "暂无该菜单权限",
          duration: 2.5,
        });
      } else if (findItem && findItem.name == "首页") {
        if (menuList[0].name == "首页") {
          routerObj["path"] = "/manage";
        } else {
          routerObj["path"] = "/tables";
          routerObj["query"] = {
            id: getMenuActiveIds(menuList),
          };
        }
      } else if (findItem && findItem.name != "首页") {
        let findItem = menuArr.find((val) => val.id == o.query.id);
        if (Array.isArray(findItem.child) && findItem.child.length > 0) {
          routerObj["path"] = "/tables";
          routerObj["query"] = {
            id: getMenuActiveIds(findItem.child),
          };
        }
      }
    }
  }
  if (Object.keys(JSON.parse(JSON.stringify(routerObj))).length > 0) {
    t.$router.replace(routerObj);
  }
};
const tempColumn = {
  initiatingPieChartUn: [
    // {
    //     title: 'ID',
    //     dataIndex: 'id',
    //     align: 'center',
    //     // ellipsis: true,
    //     // fixed: true,
    //     width: 200,
    // },
    // {
    //     title: '工单名称',
    //     dataIndex: 'titleName',
    //     align: 'center',
    //     // ellipsis: true,
    //     width: 200,
    //     // fixed: 'left',
    // },
    {
      title: "流程名称",
      dataIndex: "titleName",
      align: "center",
      //   ellipsis: true,
      // ellipsis: true,
      //   width: 100,
    },
    // {
    //     title: '完结时间',
    //     dataIndex: 'finishTime',
    //     align: 'center',
    //     // ellipsis: true,
    //     width: 200,
    // },
    {
      title: "发起人",
      dataIndex: "userName",
      align: "center",
      // ellipsis: true,
      width: 80,
    },
    {
      title: "发起部门",
      dataIndex: "originatorDeptName",
      align: "center",
      ellipsis: true,
      //   width: 140,
      // ellipsis: true,
      // width: 300,
    },
    {
      title: "工单状态",
      dataIndex: "status",
      align: "center",
      // ellipsis: true,
      width: 100,
      ellipsis: true,
    },
    // {
    //   title: "办理时间",
    //   dataIndex: "bizData",
    //   align: "center",
    //   // ellipsis: true,
    //   width: 100,
    //   // sorter: true,
    // },
    {
      title: "发起时间",
      dataIndex: "createTime",
      align: "center",
      // ellipsis: true,
      //   width: 120,
      // sorter: true,
    },
    {
      title: "操作",
      dataIndex: "options",
      align: "center",
      width: 80,
      fixed: "right",
      scopedSlots: { customRender: "options" },
    },
  ],
};
//
const chineseCharactersNum = (num) => {
  let res;
  let arr = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
  let nowN = num;

  if (nowN > 101) {
    nowN = nowN - 100;
  }
  if (nowN + 1 <= arr.length) {
    res = arr[nowN];
  } else {
    // 十位
    let shiwei = Math.floor((nowN + 1) / arr.length);
    // 各位
    let gewei = nowN % arr.length;
    res = `${shiwei > 1 ? arr[shiwei - 1] : ""}十${gewei != 9 ? arr[gewei] : ""}`;
  }
  return res;
};
const staticPageSizeOptions = ["10", "50", "100", "200"];
export { locationF, routeAuthority, tempColumn, chineseCharactersNum, staticPageSizeOptions };
